package swd.basic.conf_prg_info;

import com.pub.database.*;
import java.util.*;

public class Business {
   QueryBean gqbnDAO=null;
   public Business(QueryBean pqbnDAO){
      gqbnDAO=pqbnDAO;
   }
   String selectTableDataToFld(View view) throws Exception {
      String lstrSQL=null;
      Hashtable<String,Object> lhtTableData=null;
      try{
         lstrSQL="select dbf_no,prg_no,btn_pos,flow_yn,join_sys,prg_kind,prg_name," +
                 "       flow_code,flow_copy,join_group,prg_name_e" +
                 "  from wkf_prg where prg_no='" + view.getprg_no() + "'";
         lhtTableData=gqbnDAO.readSingleRowData(lstrSQL);
         if(lhtTableData==null){
            return "查無資料";
         }
         view.setdbf_no(String.valueOf((lhtTableData.get("DBF_NO").toString())));
         view.setprg_no(String.valueOf((lhtTableData.get("PRG_NO").toString())));
         view.setbtn_pos(String.valueOf((lhtTableData.get("BTN_POS").toString())));
         view.setflow_yn(String.valueOf((lhtTableData.get("FLOW_YN").toString())));
         view.setjoin_sys(String.valueOf((lhtTableData.get("JOIN_SYS").toString())));
         view.setprg_kind(String.valueOf((lhtTableData.get("PRG_KIND").toString())));
         view.setprg_name(String.valueOf((lhtTableData.get("PRG_NAME").toString())));
         view.setflow_code(String.valueOf((lhtTableData.get("FLOW_CODE").toString())));
         view.setflow_copy(String.valueOf((lhtTableData.get("FLOW_COPY").toString())));
         view.setjoin_group(String.valueOf((lhtTableData.get("JOIN_GROUP").toString())));
         view.setprg_name_e(String.valueOf((lhtTableData.get("PRG_NAME_E").toString())));
         setPrgKindCheckBox(view);

         return "";
      }
      catch(Exception e){
         throw new Exception(e.toString());
      }
      finally{
         lhtTableData.clear();
         lhtTableData=null;
      }
   }
   String exeUpdSQL(View view) throws Exception{
      String lstrSQL=new String();
      Vector<Object> lvtrParam = new Vector<Object>();
      try{
         lvtrParam.add(view.getdbf_no()==null ? null :String.valueOf(view.getdbf_no()));
         lvtrParam.add(view.getprg_no()==null ? null :String.valueOf(view.getprg_no()));
         lvtrParam.add(view.getbtn_pos()==null ? null :String.valueOf(view.getbtn_pos()));
         lvtrParam.add(view.getflow_yn()==null ? null :String.valueOf(view.getflow_yn()));
         lvtrParam.add(view.getjoin_sys()==null ? null :String.valueOf(view.getjoin_sys()));
         lvtrParam.add(view.getprg_kind()==null ? null :String.valueOf(view.getprg_kind()));
         lvtrParam.add(view.getprg_name()==null ? null :String.valueOf(view.getprg_name()));
         lvtrParam.add(view.getflow_code()==null ? null :String.valueOf(view.getflow_code().toUpperCase()));
         lvtrParam.add(view.getflow_copy()==null ? null :String.valueOf(view.getflow_copy()));
         lvtrParam.add(view.getjoin_group()==null ? null :String.valueOf(view.getjoin_group()));
         lvtrParam.add(view.getprg_name_e()==null ? null :String.valueOf(view.getprg_name_e()));
         lstrSQL="update wkf_prg " +
                 "   set dbf_no=?,prg_no=?,btn_pos=?,flow_yn=?,join_sys=?," +
                 "       prg_kind=?,prg_name=?,flow_code=?,flow_copy=?,join_group=?,prg_name_e=? " +
                 " where prg_no='" + view.getprg_no() + "'";
         if(!gqbnDAO.executeSQL(lstrSQL,lvtrParam)){
            return "wkf_prg";
         }
         return "";
      }
      catch(Exception e){
         e.printStackTrace();
         throw new Exception(e.toString());
      }
      finally{
         lvtrParam.clear();
      }
   }
   String exeInsSQL(View view) throws Exception{
      String lstrSQL=new String();
      Vector<Object> lvtrParam = new Vector<Object>();
      String strExitYn="0";
      try{
         strExitYn = gqbnDAO.readSingleValueData("select count(*) from wkf_prg" +
                                                   " where prg_no='" + view.getprg_no() + "'");
         if (strExitYn.equals("0")){
            lvtrParam.add((view.getprg_no()==null ? null : view.getprg_no()));
            lvtrParam.add((view.getprg_name()==null ? null : view.getprg_name()));
            lvtrParam.add((view.getdbf_no()==null ? null : view.getdbf_no()));
            lvtrParam.add((view.getjoin_sys()==null ? null : view.getjoin_sys()));
            lvtrParam.add((view.getjoin_group()==null ? null : view.getjoin_group()));
            lvtrParam.add((view.getflow_yn()==null ? null : view.getflow_yn()));
            lvtrParam.add((view.getprg_kind()==null ? null : view.getprg_kind()));
            lvtrParam.add((view.getbtn_pos()==null ? null : view.getbtn_pos()));
            lvtrParam.add((view.getflow_code()==null ? null : view.getflow_code().toUpperCase()));
            lvtrParam.add((view.getflow_copy()==null ? null : view.getflow_copy()));
            lvtrParam.add((view.getprg_name_e()==null ? null : view.getprg_name_e()));
            lstrSQL="insert into wkf_prg " +
                    "  (prg_no,prg_name,dbf_no,join_sys,join_group,flow_yn," +
                    "   prg_kind,btn_pos,flow_code,flow_copy,prg_name_e)" +
                    " values (?,?,?,?,?,?,?,?,?,?,?)";
            if(!gqbnDAO.executeSQL(lstrSQL,lvtrParam)){;
               return "wkf_prg";
            }
         } else {
             return view.getprg_no().toString() + "重複!!";
         }
         lvtrParam.clear();

         return "";
      }
      catch(Exception e){
         e.printStackTrace();
         throw new Exception(e);
      }
      finally{
         lvtrParam.clear();
      }
   }

   boolean exeUpdSQL(View view,int pintSn){
      String lstrSQL=new String();
      Vector<Object> lvtrParam = new Vector<Object>();
      try{
          saveDataToWkf_Sys_Prg(view.getjoin_sys_g()[pintSn],view.getprg_no_g()[pintSn]);
          saveDataToWkf_Prg_Right(view.getjoin_group_g()[pintSn],view.getprg_no_g()[pintSn]);
          lvtrParam.add(view.getdbf_no_g()[pintSn]==null ? null :String.valueOf(view.getdbf_no_g()[pintSn]));
          lvtrParam.add(view.getprg_no_g()[pintSn]==null ? null :String.valueOf(view.getprg_no_g()[pintSn]));
          lvtrParam.add(view.getflow_yn_g()[pintSn]==null ? null :String.valueOf(view.getflow_yn_g()[pintSn]));
          lvtrParam.add(view.getjoin_sys_g()[pintSn]==null ? null :String.valueOf(view.getjoin_sys_g()[pintSn]));
          lvtrParam.add(view.getprg_kind_g()[pintSn]==null ? null :String.valueOf(view.getprg_kind_g()[pintSn]));
          lvtrParam.add(view.getprg_name_g()[pintSn]==null ? null :String.valueOf(view.getprg_name_g()[pintSn]));
          lvtrParam.add(view.getflow_code_g()[pintSn]==null ? null :String.valueOf(view.getflow_code_g()[pintSn].toUpperCase()));
          lvtrParam.add(view.getjoin_group_g()[pintSn]==null ? null :String.valueOf(view.getjoin_group_g()[pintSn]));
          lvtrParam.add(view.getprg_name_e_g()[pintSn]==null ? null :String.valueOf(view.getprg_name_e_g()[pintSn]));
          lstrSQL="update wkf_prg " +
                  "   set dbf_no=?,prg_no=?,flow_yn=?,join_sys=?," +
                  "       prg_kind=?,prg_name=?,flow_code=?,join_group=?,prg_name_e=? " +
                  " where prg_no='" + view.getprg_no_g()[pintSn] + "'";

         return gqbnDAO.executeSQL(lstrSQL,lvtrParam);
      }
      catch(Exception e){
         e.printStackTrace();
         return false;
      }
      finally{
         lvtrParam.clear();
      }
   }


   void exeDelSQL(View view,int pintSn){
      String lstrSQL=new String();
      Vector<Object> lvtrParam = new Vector<Object>();
      try{
         lstrSQL="delete wkf_prg " +
                 " where prg_no='" + view.getprg_no_g()[pintSn] + "'";
         gqbnDAO.executeSQL(lstrSQL);
      } catch(Exception e){
         e.printStackTrace();
      } finally{
          lvtrParam.clear();
      }
   }

   String batchUpdateData(View view) throws Exception{
       String[] lstrRowSn=view.getrowsn().split(",");

       try{
           for(int i=0;i<lstrRowSn.length;i++){
               if(!exeUpdSQL(view,Integer.parseInt(lstrRowSn[i]))){
                   throw new Exception("update error");
               }
           }
           return "儲存成功!";
       }
       catch(Exception e){
           return "儲存失敗!!";
       }
   }


   void setPrgKindCheckBox(View view) throws Exception{
      // 取得程式的種類
      // D:新增 Y:簽核 R:查詢 K:外掛或其它
      view.setprg_kind0("");
      view.setprg_kind1("");
      view.setprg_kind2("");
      view.setprg_kind3("");
      if (view.getprg_kind()!=null){
         switch (view.getprg_kind().charAt(0)) {
         case 'D':
             view.setprg_kind0("D");
             break;
         case 'Y':
             view.setprg_kind1("Y");
             break;
         case 'R':
             view.setprg_kind2("R");
             break;
         case 'K':
             view.setprg_kind3("K");
             break;
         }
      }
   }

   String setPrgNoByPrgKind(View view) throws Exception{
      // 依使用者所選取的程式類別，設定其代號
      // D:新增 Y:簽核 R:查詢 K:外掛或其它
      String ReturnValue="";
      String strPrgNo=view.getprg_no();
      String strReturn="";
      String strPrgName=view.getprg_name();
      String strPrgNameE=view.getprg_name_e();
      if (view.getprg_kind0()!=null&&view.getprg_kind0().equals("D")){
          view.setprg_kind("D");
          view.setprg_no(strPrgNo+ "_add");
          view.setprg_name(strPrgName + "-新增頁");
          view.setprg_name_e(strPrgNameE + "-Add");

          ReturnValue=exeInsSQL(view);
          if (ReturnValue.equals("")){
             saveDataToWkf_Sys_Prg(view.getjoin_sys(),view.getprg_no());
             saveDataToWkf_Prg_Right(view.getjoin_group(),view.getprg_no());
             strReturn += view.getprg_no() + "新增成功!! \\n";
          } else {
             strReturn += ReturnValue + " \\n";
          }
      }

      if (view.getprg_kind1()!=null&&view.getprg_kind1().equals("Y")){
         view.setprg_kind("Y");
         view.setprg_no(strPrgNo+ "_app");
         view.setprg_name(strPrgName + "-簽核頁");
          view.setprg_name_e(strPrgNameE + "-App");
         ReturnValue=exeInsSQL(view);
         if (ReturnValue.equals("")){
            saveDataToWkf_Sys_Prg(view.getjoin_sys(),view.getprg_no());
            saveDataToWkf_Prg_Right(view.getjoin_group(),view.getprg_no());
            strReturn += view.getprg_no() + "新增成功!! \\n";
          } else {
             strReturn += ReturnValue + " \\n";
          }
     }

     if (view.getprg_kind2()!=null&&view.getprg_kind2().equals("R")){
         view.setprg_kind("R");
         view.setprg_no(strPrgNo+ "_qry");
         view.setprg_name(strPrgName + "-查詢頁");
          view.setprg_name_e(strPrgNameE + "-Qry");
         ReturnValue=exeInsSQL(view);
         if (ReturnValue.equals("")){
            saveDataToWkf_Sys_Prg(view.getjoin_sys(),view.getprg_no());
            saveDataToWkf_Prg_Right(view.getjoin_group(),view.getprg_no());
            strReturn += view.getprg_no() + "新增成功!! \\n";
         } else {
            strReturn += ReturnValue + " \\n";
         }
     }

     if (view.getprg_kind3()!=null&&view.getprg_kind3().equals("K")){
         view.setprg_kind("K");
         view.setprg_no(strPrgNo);
         view.setprg_name(strPrgName);
         view.setprg_name_e(strPrgNameE);
         ReturnValue=exeInsSQL(view);
         if (ReturnValue.equals("")){
            saveDataToWkf_Sys_Prg(view.getjoin_sys(),view.getprg_no());
            saveDataToWkf_Prg_Right(view.getjoin_group(),view.getprg_no());
            strReturn += view.getprg_no() + "新增成功!! \\n";
         } else {
            strReturn += ReturnValue + " \\n";
         }
     }
     view.setprg_no(strPrgNo);
     view.setprg_name(strPrgName);
     view.setprg_name_e(strPrgNameE);
     return strReturn;
   }

   void saveDataToWkf_Sys_Prg(String strSysNo,String strPrgNo){
       // 自動加入系統代號至wkf_sys_prg
       String strExitYn="0";
       String [] ArraySysno;
       String lstrSQL=new String();
       Vector<Object> lvtrParam = new Vector<Object>();
       ArraySysno=strSysNo.split(",");
       for (int i=0;i<=ArraySysno.length-1;i++){
          strExitYn = gqbnDAO.readSingleValueData("select count(*) from wkf_sys_prg" +
                                                  " where sys_no='" + ArraySysno[i] + "'" +
                                                  "   and prg_no='" + strPrgNo + "'");
          if (strExitYn.equals("0")){
             try{
                lvtrParam.add(ArraySysno[i]);
                lvtrParam.add("0001");
                lvtrParam.add(strPrgNo);
                lstrSQL="insert into wkf_sys_prg " +
                        "  (sys_no,seqno,prg_no)" +
                        " values (?,?,?)";
                gqbnDAO.executeSQL(lstrSQL,lvtrParam);
                lvtrParam.clear();
             } catch(Exception e){
                e.printStackTrace();
             } finally{
                lvtrParam.clear();
             }
          }
       }
   }

   void saveDataToWkf_Prg_Right(String strGroupNo,String strPrgNo){
       // 自動加入群組代號至wkf_prg_right
       String strExitYn="0";
       String [] ArrayGroupNo;
       String lstrSQL=new String();
       Vector<Object> lvtrParam = new Vector<Object>();
       ArrayGroupNo=strGroupNo.split(",");
       for (int i=0;i<=ArrayGroupNo.length-1;i++){
          strExitYn = gqbnDAO.readSingleValueData("select count(*) from wkf_prg_right" +
                                                  " where prg_no='" + strPrgNo + "'" +
                                                  "   and group_no='" + ArrayGroupNo[i] + "'");
          if (strExitYn.equals("0")){
             try{
                lvtrParam.add(strPrgNo);
                lvtrParam.add(ArrayGroupNo[i]);
                lvtrParam.add("Y");
                lvtrParam.add("Y");
                lvtrParam.add("N");
                lvtrParam.add("Y");
                lvtrParam.add("Y");
                lvtrParam.add("Y");
                lvtrParam.add("N");
                lvtrParam.add("N");
                lstrSQL="insert into wkf_prg_right " +
                        "  (prg_no,group_no,p1,p2,p3,p4,p5,p6,p7,p8)" +
                        " values (?,?,?,?,?,?,?,?,?,?)";
                gqbnDAO.executeSQL(lstrSQL,lvtrParam);
                lvtrParam.clear();
             } catch(Exception e){
                e.printStackTrace();
             } finally{
                lvtrParam.clear();
             }
          }
       }
   }



   void setDataViewForm(DataViewForm DataViewForm,View view,String pstrLoginUser) throws Exception{

       //StringBuffer stbCondition = new StringBuffer();
       StringBuffer stbPageActionURI = new StringBuffer("conf_prg_info.jsp?rowsn=");
       //rowSn

       DataViewForm.setFieldsList(" '刪',prg_no,flow_code,flow_yn,prg_kind,join_sys," +
                                  "join_group,dbf_no,prg_name,prg_name_e");
       DataViewForm.setTablesList(" wkf_prg ");
       DataViewForm.setSQLCondition("prg_no in ('" + view.getprg_no() + "'," +
                                    "           '" + view.getprg_no() + "_add'," +
                                    "           '" + view.getprg_no() + "_app'," +
                                    "           '" + view.getprg_no() + "_qry') ");
       DataViewForm.setOrderByFields("prg_no");
       DataViewForm.setOrderBySeq("asc");
       DataViewForm.setGroupBy(null);
       DataViewForm.setHaving(null);

       String strColRelation[][] = new String[10][];

       strColRelation[0] = new String[5];
       strColRelation[1] = new String[5];
       strColRelation[2] = new String[5];
       strColRelation[3] = new String[5];
       strColRelation[4] = new String[5];
       strColRelation[5] = new String[5];
       strColRelation[6] = new String[5];
       strColRelation[7] = new String[5];
       strColRelation[8] = new String[5];
       strColRelation[9] = new String[5];

       strColRelation[0][0]="''";
       strColRelation[0][1]="INPUT";
       strColRelation[0][2]="WIDTH='5%' align='center'";
       strColRelation[0][3] ="<INPUT class='button b_normal' TYPE=\"BUTTON\" onclick=\"javascript:saveRowSn('%sn%');document.all.item('prgAction').value='delete';document.forms[0].submit();\" NAME=\"del\" VALUE=";
       strColRelation[0][4]="0";

       strColRelation[1][0]="'程式編號'";
       strColRelation[1][1]="INPUT";
       strColRelation[1][2]="WIDTH='6%'";
       strColRelation[1][3]="<INPUT TYPE='text' size='6' name='prg_no_g' readonly CLASS='input_noborder' VALUE=";
       strColRelation[1][4]="1";

       strColRelation[2][0]="'單別'";
       strColRelation[2][1]="INPUT";
       strColRelation[2][2]="WIDTH='4%'";
       strColRelation[2][3]="<INPUT TYPE='text' size='3' name='flow_code_g' CLASS='input' onchange=\"javascript:saveRowSn('%sn%');\" VALUE=";
       strColRelation[2][4]="2";

       strColRelation[3][0]="'流程'";
       strColRelation[3][1]="SELECT";
       strColRelation[3][2]="WIDTH='10%' align='center'";
       strColRelation[3][3]="<SELECT CLASS='select' NAME='flow_yn_g' onchange=\"javascript:saveRowSn('%sn%');\">";
       strColRelation[3][4]="<option value=\"Y\">是</option>," +
                            "<option value=\"N\">否</option>";

       strColRelation[4][0]="'程式類別'";
       strColRelation[4][1]="SELECT";
       strColRelation[4][2]="WIDTH='6%' align='center'";
       strColRelation[4][3]="<SELECT CLASS='select' NAME='prg_kind_g' onchange=\"javascript:saveRowSn('%sn%');\">";
       strColRelation[4][4]="<option value=\"D\">新增頁</option>," +
                            "<option value=\"Y\">簽核頁</option>," +
                            "<option value=\"R\">查詢頁</option>," +
                            "<option value=\"K\">外掛</option>";

       strColRelation[5][0]="'加入系統'";
       strColRelation[5][1]="INPUT";
       strColRelation[5][2]="WIDTH='8%' ";
       strColRelation[5][3]="<INPUT TYPE='text' size='8' name='join_sys_g' onDblClick=\"javascript:fc_get_sys_data(this);saveRowSn('%sn%');\" CLASS='input_window hand' onchange=\"javascript:saveRowSn('%sn%');\" VALUE=";
       strColRelation[5][4]="5";

       strColRelation[6][0]="'加入群組'";
       strColRelation[6][1]="INPUT";
       strColRelation[6][2]="WIDTH='8%' ";
       strColRelation[6][3]="<INPUT TYPE='text' size='8' name='join_group_g' onDblClick=\"javascript:fc_get_group_data(this);saveRowSn('%sn%');\" CLASS='input_window hand' onchange=\"javascript:saveRowSn('%sn%');\" VALUE=";
       strColRelation[6][4]="6";

       strColRelation[7][0]="'異動檔案'";
       strColRelation[7][1]="INPUT";
       strColRelation[7][2]="WIDTH='10%' ";
       strColRelation[7][3]="<INPUT TYPE='text' size='10' name='dbf_no_g' onDblClick=\"javascript:fc_get_table_data(this);saveRowSn('%sn%');\" CLASS='input_window hand' onchange=\"javascript:saveRowSn('%sn%');\" VALUE=";
       strColRelation[7][4]="7";

       strColRelation[8][0]="'中文名稱'";
       strColRelation[8][1]="INPUT";
       strColRelation[8][2]="WIDTH='20%' ";
       strColRelation[8][3]="<INPUT TYPE='text' size='20' name='prg_name_g' CLASS='input' onchange=\"javascript:saveRowSn('%sn%');\" VALUE=";
       strColRelation[8][4]="8";

       strColRelation[9][0]="'英文名稱'";
       strColRelation[9][1]="INPUT";
       strColRelation[9][2]="WIDTH='23%' ";
       strColRelation[9][3]="<INPUT TYPE='text' size='25' name='prg_name_e_g' CLASS='input' onchange=\"javascript:saveRowSn('%sn%');\" VALUE=";
       strColRelation[9][4]="9";

       DataViewForm.setColRelation(strColRelation);//將欄位相關設定殖入ActionForm
       DataViewForm.setJumpPageButtonShow(false);   //是否要秀出上下頁按鈕
       DataViewForm.setPageBtnOnTop(false);         //將button and rows info 秀在頂端
       DataViewForm.setPageBtnOnButtom(true);      //將button and rows info 秀在底端
       DataViewForm.setFilterShow(false);           //是否要秀出過濾選擇欄位
       DataViewForm.setJumpPageShow(false);         //是否要秀出跳頁下拉式選單
       DataViewForm.setRowsInfoShow(false);         //是否要秀出筆數資訊
       DataViewForm.setRowPageShow(false);          //是否要秀出每頁筆數控制欄
       DataViewForm.setExcelShow(false);           //是否要秀出轉Excel button
       DataViewForm.setOrderEnable(false);          //是否要提供排序點選功能
       DataViewForm.setShowHeaderWhenNoData(false);//當ResultSet無資料是否要秀Header

       DataViewForm.setTableHeaderHtml("class='td_head_grid' align='center'  width='680px'");
             //設定上一頁與下一頁按鈕的form action(通常為母頁即本頁)
       DataViewForm.setPageActionURI(stbPageActionURI.toString());
   }


   String checkNotNullFld(View view) throws Exception{
      StringBuffer lstbNullFldList=new StringBuffer();
      return (lstbNullFldList.toString().equals("") ? "" : lstbNullFldList.delete(lstbNullFldList.length()-1,lstbNullFldList.length()).toString());
   }
   void setInitValueForAdd(View view,String pstrUserID) throws Exception{
       view.setbtn_pos("T");

   }
   void setReadonlyForAdd(View view) throws Exception{

   }

}
